import time

import numpy as np
from numba import jit


@jit
def sum_array(arr):
    result = 0.0
    for i in arr:
        result += i
    return result


def sum_array2(arr):
    result = 0.0
    for i in arr:
        result -= i
    return result


# 将 sum_array 函数单独调用一次，预热 JIT 编译器，以避免计时中包含编译时间
sum_array(np.random.rand(2))

arr = np.random.rand(1000000)
t1 = time.time()
print(sum_array(arr))  # 使用 JIT 提升性能
t2 = time.time()
print(f'时长: {t2-t1:.6f} 秒')
print(sum_array2(arr))
t3 = time.time()
print(f'时长: {t3-t2:.6f} 秒')
